from django.shortcuts import render,HttpResponse
from django import forms
from django.core.exceptions import ValidationError

# Create your views here.


def login(request):
	return render(request,'backend-login.html')

def register(request):

	from backend.authForm import RegisterForm
	from backend import models
	import json


	ret = {'isFlags':True,'message':None,'data':None}

	if request.method == 'GET':
		regobj = RegisterForm()
		return render(request,'backend-register.html',{'registerObj':regobj})

	elif request.method == 'POST':
		regobj = RegisterForm(request.POST)
		if regobj.is_valid():  #is_valid()是form对象的内置方法，主要逐一判断是否满足条件，全部通过为true
			#models.Users.objects.create(**regobj.cleaned_data)#因为form组件传递过来的字段和数据库模型的字段不一样，所以需要修改
			user = regobj.cleaned_data['username']
			passwd = regobj.cleaned_data['password']
			email = regobj.cleaned_data['email']
			dept = regobj.cleaned_data['dept']
			print(user)
			print(passwd)
			#models.Users.objects.create(name=user,passwd=passwd,email=email,department=dept)
			ret['data'] = json.dumps(regobj.cleaned_data)
			print(regobj.cleaned_data)
			print(type(regobj.cleaned_data))
		else:
			ret['isFlags'] = False
			ret['message'] = regobj.errors.as_json()
		return HttpResponse(json.dumps(ret))